package MapReduce.Demo9_Grouping_Gender;

import MapReduce.writableBean.GenderWritable;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;

/**
 * @Author lixinlei
 * @Date 2023/3/21 11:27
 */
public class GenderSortComparator extends WritableComparator {

    public GenderSortComparator(){
        super(GenderWritable.class,true);
    }

    @Override
    public int compare(WritableComparable a, WritableComparable b) {
        GenderWritable aBean = (GenderWritable) a;
        GenderWritable bBean = (GenderWritable) b;

        String aGroupName = aBean.getGroupName();
        String bGroupName = bBean.getGroupName();

        String aPersonName = aBean.getPersonName();
        String bPersonName = bBean.getPersonName();

        int aAge = aBean.getAge();
        int bAge = bBean.getAge();

        if(aGroupName.equals(bGroupName)){
            if(aAge == bAge){
                return aPersonName.compareTo(bPersonName);
            }else{
                return aAge-bAge;
            }
        }else{
            return aGroupName.compareTo(bGroupName);
        }
    }
}
